use crate::{
    facade::{builder::Builder, user::UserFacade},
    middleware::auth::Auth,
    payload::user::UserCreatePayload,
    provider::user::UserProvider,
    system::{error::ErrorCode, web_ret::WebRet},
};
use axum::{extract::Path, Json};
use serde_json::{json, Value};
/**
 * index
 */
pub(crate) async fn list(auth: Auth) -> Json<Value> {
    //
    let data = Some(json!({"self":auth.user}));
    //
    Json(json!(WebRet::success(data)))
}
/**
 * index
 */
pub(crate) async fn find(Path(user_id): Path<String>) -> Json<Value> {
    let user_info_ret = UserProvider::builder().get_by_user_id(user_id).await;
    if user_info_ret.is_err() {
        return Json(json!(ErrorCode::SYSTEM_ERROR_DB
            .to_web_ret()
            .append_msg(format!("{:?}", user_info_ret.err()))));
    }
    let data = Some(json!({"user_info":user_info_ret.unwrap()}));
    Json(json!(WebRet::success(data)))
}
/**
 * create
 */
pub(crate) async fn create(_auth: Auth, Json(payload): Json<UserCreatePayload>) -> Json<Value> {
    let user_info_ret = UserProvider::builder().create(payload).await;
    if user_info_ret.is_err() {
        return Json(json!(ErrorCode::SYSTEM_ERROR_DB
            .to_web_ret()
            .append_msg(format!("{:?}", user_info_ret.err()))));
    }
    let data = Some(json!({"user_info":user_info_ret.unwrap()}));
    Json(json!(WebRet::success(data)))
}
/**
 * delete
 */
pub(crate) async fn delete(Path(user_id): Path<String>) -> Json<Value> {
    let user_info_ret = UserProvider::builder().get_by_user_id(user_id).await;
    if user_info_ret.is_err() {
        return Json(json!(ErrorCode::SYSTEM_ERROR_DB
            .to_web_ret()
            .append_msg(format!("{:?}", user_info_ret.err()))));
    }
    let data = Some(json!({"user_info":user_info_ret.unwrap()}));
    Json(json!(WebRet::success(data)))
}
